package ecoiot.com.testproject.menu;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.nineoldandroids.view.ViewHelper;

/**
 * Created by developer on 17-8-2.
 */

public class TestSlideMenu extends HorizontalScrollView {

    private Context context;

    // 侧滑菜单距离屏幕右端的距离,默认单位是dp
    private int mMenuRightPadding = 50;

    private int mScreenWidth;

    private int mMenuWidth;
    private int mHalfMenuWidth;

    // 是否为第一次调用OnMeasure
    private boolean once;
    // 菜单是否为打开状态
    private boolean isOpen;


    private LinearLayout mainGroup;
    private LinearLayout menuLayout;
    private LinearLayout mainLayout;
    private BaseAdapter adapter;

    public TestSlideMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        getScreenWidth();
    }

    public TestSlideMenu(Context context) {
        super(context);
        this.context = context;
    }


    /**
     * 设定宽度
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        // 只有第一次调用onMeasure时才去定义菜单和主界面布局的宽度
        if(!once) {
            // HorizontalScrollView 的子布局
            mainGroup = (LinearLayout) getChildAt(0);

            // 表示侧滑菜单的布局
            menuLayout = (LinearLayout) mainGroup.getChildAt(0);
            // 表示主界面的布局
            mainLayout = (LinearLayout) mainGroup.getChildAt(1);

            // dp转化为px单位
            mMenuRightPadding = (int) TypedValue.applyDimension(
                    TypedValue.COMPLEX_UNIT_DIP, mMenuRightPadding, mainLayout
                            .getResources().getDisplayMetrics());

            // 菜单布局的宽度就是屏幕宽度减去距屏幕右端的距离
            mMenuWidth = mScreenWidth - mMenuRightPadding;
            menuLayout.getLayoutParams().width = mMenuWidth;
            mHalfMenuWidth = mMenuWidth / 2;

            // 主界面布局就是屏幕的宽度
            mainLayout.getLayoutParams().width = mScreenWidth;

            if (adapter != null) {
                ListView listView = (ListView) menuLayout.getChildAt(0);
                listView.setAdapter(adapter);
            }
       }

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // 在view给其子view设置尺寸和位置时被调用
        super.onLayout(changed, l, t, r, b);
        // changed 表示view的子view的长度和宽度是否改变
        if (changed)
        {
            // 如果改变,说明已经定义过主界面和菜单的宽度
            this.scrollTo(mMenuWidth, 0);
            once = true;
        }
    }

    /**
     * 手指滑动抬起时，进行判断，如果显示区域大于菜单宽度一半则完全显示，否则隐藏
     * @param ev
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        int action = ev.getAction();
        switch (action)
        {
            case MotionEvent.ACTION_UP:
                int scrollX = getScrollX();
                if (scrollX > mHalfMenuWidth) {
                    this.smoothScrollTo(mMenuWidth, 0);
                    isOpen = false;
                } else {
                    this.smoothScrollTo(0, 0);
                    isOpen = true;
                }
                mOnMenuStateChangedListener.onStateChanged(isOpen);
                return true;
        }


        return super.onTouchEvent(ev);
    }

    public void setMenuAdapter(BaseAdapter adapter) {
        this.adapter = adapter;
    }

    private void getScreenWidth() {
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        mScreenWidth = outMetrics.widthPixels;
    }


    public boolean changeMenuState() {
        if (isOpen) {
            this.smoothScrollTo(mMenuWidth, 0);
            isOpen = false;
        }else {
            this.smoothScrollTo(0, 0);
            isOpen = true;
        }

        return isOpen;
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        // l是X轴偏移量 t是Y轴偏移量
        float scale = l * 1.0f / mMenuWidth;
        float leftScale = 1 - 0.3f * scale;
        float rightScale = 0.8f + scale * 0.2f;

        // 设置菜单页面出现及消失时的X,Y轴缩放的比例 和 透明度变化的比例（属性动画）
        /*ViewHelper.setScaleX(menuLayout, leftScale);
        ViewHelper.setScaleY(menuLayout, leftScale);
        ViewHelper.setAlpha(menuLayout, 0.6f + 0.4f * (1 - scale));*/

        // 设置菜单页面在主页面下层,即被盖住的比例
        ViewHelper.setTranslationX(menuLayout, mMenuWidth * scale * 0.6f);

        // 设置主页面出现及消失时的X,Y轴缩放的比例（属性动画）
        /*ViewHelper.setPivotX(mainLayout, 0);
        ViewHelper.setPivotY(mainLayout, mainLayout.getHeight() / 2);
        ViewHelper.setScaleX(mainLayout, rightScale);
        ViewHelper.setScaleY(mainLayout, rightScale);*/

    }

    public void setOnMenuStateChangedListener(OnMenuStateChangedListener onMenuStateChangedListener) {
        this.mOnMenuStateChangedListener = onMenuStateChangedListener;
    }

    private OnMenuStateChangedListener mOnMenuStateChangedListener;

    public interface OnMenuStateChangedListener {
        void onStateChanged(boolean isMenuShow);
    }

}
